package com.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {

    public static DecimalFormat decimalFormat(int decimal) {
        switch (decimal) {
            case 0:
                return new DecimalFormat("##0");//decimalFormat0;
            case 1:
                return new DecimalFormat("##0.0");//decimalFormat1;
            case 2:
                return new DecimalFormat("##0.00");//decimalFormat2;
            case 3:
                return new DecimalFormat("##0.000");//decimalFormat3;
            case 4:
                return new DecimalFormat("##0.0000");//decimalFormat4;
            case 5:
                return new DecimalFormat("##0.00000");//decimalFormat5;
            case 6:
                return new DecimalFormat("##0.000000");//decimalFormat6;
            case 7:
                return new DecimalFormat("##0.0000000");//decimalFormat7;
            case 8:
                return new DecimalFormat("##0.00000000");//decimalFormat8;
            case 9:
                return new DecimalFormat("##0.000000000");//decimalFormat9;
            case 10:
                return new DecimalFormat("##0.0000000000");//decimalFormat10;
        }
        return new DecimalFormat("##0.0");
    }

    public static Double round(Double data, int decimal) {
        if (data == null) return data;
        if (data.isInfinite() || data.isNaN()) return data;
        switch (decimal) {
            case 1:
                return Math.round(data * 10) * 0.1d;
            case 2:
                return Math.round(data * 100) * 0.01d;
            case 3:
                return Math.round(data * 1000) * 0.001d;
            case 4:
                return Math.round(data * 10000) * 0.0001d;
            case 5:
                return Math.round(data * 100000) * 0.00001d;
            case 6:
                return Math.round(data * 1000000) * 0.000001d;
            case 7:
                return Math.round(data * 10000000) * 0.0000001d;
            case 8:
                return Math.round(data * 100000000) * 0.00000001d;
        }

        return data;
    }

    public static String format(Number data, int decimal) {
        if (data == null) return null;
        return decimalFormat(decimal).format(data);
    }

    public static String format(double data, int decimal) {
        return decimalFormat(decimal).format(data);
    }

    public static String convert(double[][] data) {
        StringBuilder sb = new StringBuilder();
        int c = data.length;
        for (int p = 0; p < c; p++) {
            sb.append(decimalFormat(4).format(data[p][0])).append(",").append(decimalFormat(4).format(data[p][1]));
            if (p < c - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    public static String convert(double[][] data, int decimal) {
        StringBuilder sb = new StringBuilder();
        int c = data.length;
        if (c == 0) return sb.toString();
        int c1 = data[0].length;
        for (int p = 0; p < c; p++) {
            for (int p1 = 0; p1 < c1; p1++) {
                sb.append(decimalFormat(decimal).format(data[p][p1]));
                if (p1 < c1 - 1) {
                    sb.append(",");
                }
            }
            if (p < c - 1) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }

    public static String jsonArray(double[][] data, int decimal) {
        StringBuilder sb = new StringBuilder();
        int c = data.length;
        if (c == 0) return sb.toString();
        int c1 = data[0].length;
        sb.append("[");
        for (int p = 0; p < c; p++) {
            sb.append("[");
            for (int p1 = 0; p1 < c1; p1++) {
                sb.append(decimalFormat(decimal).format(data[p][p1]));
                if (p1 < c1 - 1) {
                    sb.append(",");
                }
            }
            sb.append("]");
            if (p < c - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static String convert(double[] data) {
        return convert(data, 4);
    }

    public static String convert(double[] data, int decimal) {
        StringBuilder sb = new StringBuilder();
        int c = data.length;
        for (int p = 0; p < c; p++) {
            sb.append(decimalFormat(decimal).format(data[p]));
            if (p < c - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    public static String jsonArray(double[] data, int decimal) {
        StringBuilder sb = new StringBuilder();
        int c = data.length;
        sb.append("[");
        for (int p = 0; p < c; p++) {
            sb.append(decimalFormat(decimal).format(data[p]));
            if (p < c - 1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return sb.toString();
    }

    public static String convert(List<Double> data, int decimal) {
        StringBuilder sb = new StringBuilder();
        int c = data.size();
        for (int p = 0; p < c; p++) {
            sb.append(decimalFormat(decimal).format(data.get(p)));
            if (p < c - 1) {
                sb.append(",");
            }
        }
        return sb.toString();
    }

    public static double[] convert(String data, int decimal) {
        if (StringUtil.isEmpty(data)) return null;
        String[] sdata = data.trim().split(",");
        int c = sdata.length;
        double[] result = new double[c];
        try {
            for (int p = 0; p < c; p++) {
                result[p] = decimalFormat(decimal).parse(sdata[p]).doubleValue();
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int[] convert(String data) {
        String[] sdata = data.trim().split(",");
        int c = sdata.length;
        int[] result = new int[c];

        for (int p = 0; p < c; p++) {
            result[p] = Integer.valueOf(sdata[p]);
        }

        return result;
    }

    public static double[] toDoubleArray(String data) {
        String[] sdata = data.trim().split(",");
        int c = sdata.length;
        double[] result = new double[c];

        for (int p = 0; p < c; p++) {
            if ("null".equals(sdata[p])) {
                result[p] = Double.NaN;
            } else {
                result[p] = Double.valueOf(sdata[p]);
            }
        }

        return result;
    }

    public static long[] toLongArray(String data) {
        String[] sdata = data.trim().split(",");
        int c = sdata.length;
        long[] result = new long[c];

        for (int p = 0; p < c; p++) {
            result[p] = Long.valueOf(sdata[p]);
        }

        return result;
    }

    public static double[][] toPoints(String geometry) {
        String[] sps = geometry.trim().split(" ");

        double[][] points = new double[2][sps.length];
        int i = 0;
        for (String sp : sps) {
            String[] xy = sp.split(",");
            points[0][i] = Double.parseDouble(xy[0]);
            points[1][i++] = Double.parseDouble(xy[1]);
        }
        return points;
    }

    public static double[][] toPointsN(String geometry) {
        String[] sps = geometry.trim().split(" ");

        double[][] points = new double[sps.length][sps[0].split(",").length];
        int i = 0, j = 0;
        for (String sp : sps) {
            j = 0;
            for (String xy : sp.split(",")) {
                points[i][j++] = Double.parseDouble(xy);
            }
            i++;
        }
        return points;
    }

    public double parseDouble(String v) {
        if ("null".equals(v)) return Double.NaN;
        return Double.parseDouble(v);
    }

    public static double[][] toPointsN2(String geometry) {
        String[] sps = geometry.trim().split(" ");

        double[][] points = new double[sps.length][2];
        int i = 0;
        for (String sp : sps) {
            String[] xy = sp.split(",");
            points[i][0] = Double.parseDouble(xy[0]);
            points[i++][1] = Double.parseDouble(xy[1]);
        }
        return points;
    }

    public static double[][] toArrayN2(String jsonArray) {
        Object[] as = JsonUtil.toArray(jsonArray);

        double[][] points = new double[as.length][2];
        Object[] xy = null;
        for (int i = 0; i < as.length; i++) {
            xy = (Object[]) as[i];
            points[i][0] = ((Number) xy[0]).doubleValue();
            points[i][1] = ((Number) xy[1]).doubleValue();
        }
        return points;
    }

    public static double[][] toArrayN3(String jsonArray) {
        Object[] as = JsonUtil.toArray("[" + jsonArray + "]");
        double[][] points = new double[as.length][2];
//        Object[] xy=null;
        for (int i = 0; i < as.length; i++) {
            if (as[i].toString().contains(" ")) {
                String[] split = as[i].toString().split(" ");
                points[i][0] = Double.parseDouble(split[0]);
                points[i][1] = Double.parseDouble(split[split.length - 1]);
            } else {
                points[i][0] = Double.parseDouble(as[i].toString());
            }
        }
        return points;
    }

    public static double[][] toArray2(String jsonArray) {
        Object[] as = JsonUtil.toArray(jsonArray);

        double[][] points = new double[2][as.length];
        Object[] xy = null;
        for (int i = 0; i < as.length; i++) {
            xy = (Object[]) as[i];
            points[0][i] = ((Number) xy[0]).doubleValue();
            points[1][i] = ((Number) xy[1]).doubleValue();
        }
        return points;
    }

    public static double[][] toPoints3(String geometry) {
        String[] sps = geometry.trim().split(" ");

        double[][] points = new double[3][sps.length];
        int i = 0;
        for (String sp : sps) {
            String[] xy = sp.split(",");
            points[0][i] = Double.parseDouble(xy[0]);
            points[1][i] = Double.parseDouble(xy[1]);
            points[2][i++] = Double.parseDouble(xy[2]);
        }
        return points;
    }

    public static double[][] toPointsN3(String geometry) {
        String[] sps = geometry.trim().split(" ");

        double[][] points = new double[sps.length][3];
        int i = 0;
        for (String sp : sps) {
            String[] xy = sp.split(",");
            points[i][0] = Double.parseDouble(xy[0]);
            points[i][1] = Double.parseDouble(xy[1]);
            points[i++][2] = Double.parseDouble(xy[2]);
        }
        return points;
    }

    public static double[][] toPoints(String... geometry) {
        String[] sps = geometry;

        double[][] points = new double[2][sps.length];
        int i = 0;
        for (String sp : sps) {
            String[] xy = sp.split(",");
            points[0][i] = Double.parseDouble(xy[0]);
            points[1][i++] = Double.parseDouble(xy[1]);
        }
        return points;
    }

    public static String getDecimalStringByLength(int len) {
        String string = "0";
        switch (len) {
            case 0:
                string = "0";
                break;
            case 1:
                string = "0.0";
                break;
            case 2:
                string = "0.00";
                break;
            case 3:
                string = "0.000";
                break;
            case 4:
                string = "0.0000";
                break;
            case 5:
                string = "0.00000";
                break;
            case 6:
                string = "0.000000";
                break;
            case 7:
                string = "0.0000000";
                break;
            case 8:
                string = "0.00000000";
                break;
        }
        return string;
    }

    public static Double max(Double[] values) {
        if (values.length == 1) return values[0];
        Double max = values[0];
        for (int i = 1; i < values.length; i++) {
            max = Math.max(max, values[i]);
        }
        return max;
    }

    public static Double min(Double[] values) {
        if (values.length == 1) return values[0];
        Double min = values[0];
        for (int i = 1; i < values.length; i++) {
            min = Math.min(min, values[i]);
        }
        return min;
    }

    public static Double max(List<Double> values) {
        if (values.size() == 1) return values.get(0);
        Double max = values.get(0);
        for (int i = 1; i < values.size(); i++) {
            max = Math.max(max, values.get(i));
        }
        return max;
    }

    public static Double min(List<Double> values) {
        if (values.size() == 1) return values.get(0);
        Double min = values.get(0);
        for (int i = 1; i < values.size(); i++) {
            min = Math.min(min, values.get(i));
        }
        return min;
    }

    public static Double sum(List<Double> values) {
        Double sum = 0.0;
        for (int i = 0; i < values.size(); i++) {
            sum += values.get(i);
        }
        return sum;
    }

    public static Double ave(List<Double> values) {
        Double sum = 0.0;
        for (int i = 0; i < values.size(); i++) {
            sum += values.get(i);
        }
        return sum / values.size();
    }

    public static BigDecimal bigRoot(BigDecimal number, int n, int scale, int roundingMode) {
        boolean negate = false;
        if (n < 0)
            throw new ArithmeticException();
        if (number.compareTo(BigDecimal.ZERO) < 0) {
            if (n % 2 == 0)
                throw new ArithmeticException();
            else {
                number = number.negate();
                negate = true;
            }
        }

        BigDecimal root;

        if (n == 0)
            root = BigDecimal.ONE;
        else if (n == 1)
            root = number;
        else {
            final BigInteger N = BigInteger.valueOf(n);
            final BigInteger N2 = BigInteger.TEN.pow(n);
            final BigInteger N3 = BigInteger.TEN.pow(n - 1);
            final BigInteger NINE = BigInteger.valueOf(9);

            BigInteger[] C = new BigInteger[n + 1];
            for (int i = 0; i <= n; i++) {
                C[i] = combination(n, i);
            }

            BigInteger integer = number.toBigInteger();
            String strInt = integer.toString();
            int lenInt = strInt.length();
            for (int i = lenInt % n; i < n && i > 0; i++)
                strInt = "0" + strInt;
            lenInt = (lenInt + n - 1) / n * n;
            BigDecimal fraction = number.subtract(number.setScale(0, BigDecimal.ROUND_DOWN));
            int lenFrac = (fraction.scale() + n - 1) / n * n;
            fraction = fraction.movePointRight(lenFrac);
            String strFrac = fraction.toPlainString();
            for (int i = strFrac.length(); i < lenFrac; i++)
                strFrac = "0" + strFrac;

            BigInteger res = BigInteger.ZERO;
            BigInteger rem = BigInteger.ZERO;
            for (int i = 0; i < lenInt / n; i++) {
                rem = rem.multiply(N2);

                BigInteger temp = new BigInteger(strInt.substring(i * n, i * n + n));
                rem = rem.add(temp);

                BigInteger j;
                if (res.compareTo(BigInteger.ZERO) != 0)
                    j = rem.divide(res.pow(n - 1).multiply(N).multiply(N3));
                else
                    j = NINE;
                BigInteger test = BigInteger.ZERO;
                temp = res.multiply(BigInteger.TEN);
                while (j.compareTo(BigInteger.ZERO) >= 0) {
                    //test = res.multiply(BigInteger.TEN);
                    //test = ((test.add(j)).pow(n)).subtract(test.pow(n));
                    test = BigInteger.ZERO;
                    if (j.compareTo(BigInteger.ZERO) > 0)
                        for (int k = 1; k <= n; k++)
                            test = test.add(j.pow(k).multiply(C[k]).multiply(temp.pow(n - k)));
                    if (test.compareTo(rem) <= 0)
                        break;
                    j = j.subtract(BigInteger.ONE);
                }

                rem = rem.subtract(test);
                res = res.multiply(BigInteger.TEN);
                res = res.add(j);
            }
            for (int i = 0; i <= scale; i++) {
                rem = rem.multiply(N2);

                if (i < lenFrac / n) {
                    BigInteger temp = new BigInteger(strFrac.substring(i * n, i * n + n));
                    rem = rem.add(temp);
                }

                BigInteger j;
                if (res.compareTo(BigInteger.ZERO) != 0) {
                    j = rem.divide(res.pow(n - 1).multiply(N).multiply(N3));
                } else
                    j = NINE;
                BigInteger test = BigInteger.ZERO;
                BigInteger temp = res.multiply(BigInteger.TEN);
                while (j.compareTo(BigInteger.ZERO) >= 0) {
                    //test = res.multiply(BigInteger.TEN);
                    //test = ((test.add(j)).pow(n)).subtract(test.pow(n));
                    test = BigInteger.ZERO;
                    if (j.compareTo(BigInteger.ZERO) > 0)
                        for (int k = 1; k <= n; k++)
                            test = test.add(j.pow(k).multiply(C[k]).multiply(temp.pow(n - k)));
                    if (test.compareTo(rem) <= 0)
                        break;
                    j = j.subtract(BigInteger.ONE);
                }

                rem = rem.subtract(test);
                res = res.multiply(BigInteger.TEN);
                res = res.add(j);
            }
            root = new BigDecimal(res).movePointLeft(scale + 1);
            if (negate)
                root = root.negate();
        }
        return root.setScale(scale, roundingMode);
    }

    public static BigInteger combination(int n, int k) {
        if (k > n || n < 0 || k < 0)
            return BigInteger.ZERO;
        if (k > n / 2)
            return combination(n, n - k);
        BigInteger N1 = BigInteger.ONE;
        BigInteger N2 = BigInteger.ONE;
        BigInteger N = BigInteger.valueOf(n);
        BigInteger K = BigInteger.valueOf(k);
        for (int i = 0; i < k; i++) {
            N1 = N1.multiply(N);
            N2 = N2.multiply(K);
            N = N.subtract(BigInteger.ONE);
            K = K.subtract(BigInteger.ONE);
        }
        return N1.divide(N2);
    }

    public static void main(String[] args) throws ParseException {
		/*System.out.println(decimalFormat(2).parse(decimalFormat(2).format(decimalFormat(2).parse("0"))));
		System.out.println(decimalFormat(2).parse(decimalFormat(2).format(decimalFormat(2).parse("0"))).getClass());
		BigDecimal num = new BigDecimal("1000.0001");
		long time;
		time= System.nanoTime();
		BigDecimal root = NumberUtil.bigRoot(num, 3, 1, BigDecimal.ROUND_HALF_UP);
		time = System.nanoTime() - time;
		System.out.println(root);
		System.out.println(root.pow(3));
		System.out.println(num);
		System.out.println(time);*/
        System.out.println(decimalFormat(4).format(0.92372381));
        System.out.println(decimalFormat(4).parse(decimalFormat(4).format(Double.NaN)));
    }

    public static boolean hasNull(Double[][] da) {
        if (da.length == 0) return false;
        for (int i = 0; i < da.length; i++) {
            for (int j = 0; j < da[i].length; j++) {
                if (da[i][j] == null) return true;
            }
        }
        return false;
    }

    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("-?[0-9]+(.[0-9]+)?");
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    public static String doubleToPercent(Double data, int decimal) {
        if (data == null) return null;
        if (data.isInfinite() || data.isNaN()) return null;
        switch (decimal) {
            case 0:
                return new DecimalFormat("0%").format(data);
            case 1:
                return new DecimalFormat("0.0%").format(data);
            case 2:
                return new DecimalFormat("0.00%").format(data);
            case 3:
                return new DecimalFormat("0.000%").format(data);
            case 4:
                return new DecimalFormat("0.0000%").format(data);
            case 5:
                return new DecimalFormat("0.00000%").format(data);
            case 6:
                return new DecimalFormat("0.000000%").format(data);
            case 7:
                return new DecimalFormat("0.0000000%").format(data);
            case 8:
                return new DecimalFormat("0.00000000%").format(data);
        }
        return data.toString();
    }
}
